package controller

import (
	"core/entity"
	"core/service"

	"github.com/gin-gonic/gin"
)

func createProjectGroup(c *gin.Context) {
	var projectGroup entity.ProjectGroup

	err := c.BindJSON(&projectGroup)
	if err != nil {
		SendError(c, err)
		return
	}

	err = service.CreateProjectGroup(&projectGroup)
	if err != nil {
		SendError(c, err)
		return
	}
}

func deleteProjectGroup(c *gin.Context) {
	projectGroupId := c.Param("projectGroupId")
	err := service.DeleteProjectGroup(projectGroupId)
	if err != nil {
		SendError(c, err)
		return
	}
	SendData(c, nil)
}

func updateProjectGroup(c *gin.Context) {
	var projectGroup entity.ProjectGroup

	projectGroupId := c.Param("projectGroupId")

	err := c.BindJSON(&projectGroup)
	if err != nil {
		SendError(c, err)
		return
	}

	err = service.UpdateProjectGroup(projectGroupId, &projectGroup)
	if err != nil {
		SendError(c, err)
		return
	}

	SendData(c, nil)
}

func updateShowProjectGroup(c *gin.Context) {

	projectGroupId := c.Param("projectGroupId")

	err := service.UpdateShowProjectGroup(projectGroupId)
	if err != nil {
		SendError(c, err)
		return
	}

	SendData(c, nil)
}

func ProjectGroupRegister(r *gin.Engine) {
	r.POST("/projects/groups", createProjectGroup)
	r.DELETE("/projects/groups/:projectGroupId", deleteProjectGroup)
	r.PUT("/projects/groups/:projectGroupId", updateProjectGroup)
	r.PUT("/projects/groups/:projectGroupId/is_show", updateShowProjectGroup)
}
